
<%@page import="com.cs.weixin.util.JsonBinder"%>
<%@page import="org.apache.commons.httpclient.methods.GetMethod"%>
<%@page import="org.apache.commons.httpclient.HttpClient"%>
<%@page import="com.cs.weixin.util.SystemParames"%>
<%@ page language="java" import="java.util.*"  pageEncoding="UTF-8"%>
<%
	String code = request.getParameter("code");
	 String redirect_uri = request.getParameter("redirect_uri"); 
	 /* String redirect_uri = "http://hejie.iask.in/wyshop/weixin/tab-home.html"; */ 
	StringBuffer url = new StringBuffer();
	String url2=request.getScheme()+"://"+ request.getServerName();//+request.getRequestURI();
	System.out.println("==========="+code+":"+redirect_uri);
	
	if(code != null && !code.equals("")){
		url.append("https://api.weixin.qq.com/sns/oauth2/access_token?");
		url.append("appid="+SystemParames.APP_ID+"&");	
		url.append("secret="+SystemParames.APP_SECRET+"&");
		url.append("code="+code+"&");
		url.append("grant_type=authorization_code");
		
	    HttpClient httpClient = new HttpClient();  
		GetMethod getMethod = new GetMethod(url.toString());
		httpClient.executeMethod(getMethod);
		String result = new String(getMethod.getResponseBody(), "UTF-8");  //获取返回数据
		Map responseMap = JsonBinder.buildNormalBinder().fromJson(result, Map.class); //数据转译成Map
		if(responseMap.get("openid") != null){
			 session.setAttribute("openid",responseMap.get("openid"));
			 System.out.println("=============="+session.getAttribute("openid"));
			 
		}
		System.out.println(responseMap.toString());
		if(redirect_uri != null){
			response.sendRedirect(redirect_uri+"?time="+System.currentTimeMillis());
		}else{
			response.sendRedirect(url2+"/wyshop/weChat/tab-home.html");
		}
		
	}else{
		url.append("https://open.weixin.qq.com/connect/oauth2/authorize?");
		url.append("appid="+SystemParames.APP_ID+"&");
		url.append("redirect_uri="+url2+SystemParames.REDIRECT_URI+"&");
		url.append("response_type=code&scope=snsapi_base&state=123#wechat_redirect");
		System.out.println(url);
		response.sendRedirect(url.toString());
		
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

</body>
</html>